package com.hrd.admin.task;

import com.alibaba.fastjson.JSONObject;
import com.hrd.common.util.RestTemplateUtil;
import com.hrd.datasource.DatabaseContextHolder;
import com.hrd.mybatis.bean.auto.TMLArchives;
import com.hrd.mybatis.bean.auto.TMLArchivesExample;
import com.hrd.mybatis.bean.ext.ExtWarehouse;
import com.hrd.mybatis.bean.ext.JDResult;
import com.hrd.mybatis.mapper.auto.TMLArchivesMapper;
import com.hrd.service.CommonService;
import com.hrd.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: wangpeng
 * @Date: 2021/11/3 16:00
 * @Description:
 */
@Component
public class TaskService {
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    TMLArchivesMapper tmlArchivesMapper;
    @Autowired
    CommonService commonService;

    /* @Scheduled(cron="0 0 * * * ?")*/
    public void invListTask() {
        System.out.println("=====================定时开始");
        ExtWarehouse extWarehouse = new ExtWarehouse();
        extWarehouse.setIsClick("yes");
        statisticsService.getcolumns(extWarehouse, true);
        System.out.println("=====================定时结束");
    }

    Date testDate = new Date(1578709758000l);

    /**
     * 定时同步客户信息
     */
    /* @Scheduled(cron="0 0 * * * ?")*/
//    @Transactional
    public void updateCustomerMsg() {
        DatabaseContextHolder.clearCustomerType();
        DatabaseContextHolder.setCustomerType("datasource1");
        //查询需要修改的客户信息
        TMLArchivesExample tmlArchivesExample = new TMLArchivesExample();
        tmlArchivesExample.createCriteria()
                .andFmodifydateGreaterThanOrEqualTo(testDate);
        List<TMLArchives> tmlArchives = tmlArchivesMapper.selectByExample(tmlArchivesExample);


        //构造金蝶接口参数
        String accessToken = commonService.getAccessToken().getResult();

        String url = "http://api.kingdee.com/jdy/basedata/customer_save?access_token=" + accessToken;


        for (TMLArchives t : tmlArchives) {
            JSONObject item = new JSONObject();
            item.put("number", t.getFno());
            item.put("name", t.getFname());
            item.put("bankaccount", t.getFbackno());
            List<JSONObject> contactpersons = new ArrayList<>();
            JSONObject contactperson = new JSONObject();
            contactperson.put("mobile", t.getFhandphone());
            contactperson.put("contactperson", t.getFlinkname());
            contactperson.put("isdefault_linkman", true);
            contactpersons.add(contactperson);
            item.put("contactpersons", contactpersons);
            getKingDeeInterface(item.toString(), url);

        }
    }

    /**
     * 调用金蝶接口
     * @return
     */

    public String getKingDeeInterface(String payload,String url) {
        HttpHeaders headers = new HttpHeaders();
        JDResult jdResult = commonService.authToken();

        //设置请求头使用add方法
        headers.add("Content-Type", "application/json;charset=UTF-8");
        headers.add("groupName", jdResult.getGroupName());
        headers.add("accountId", jdResult.getAccountId());
        headers.add("X-GW-Router-Addr", jdResult.getDomain());

        RestTemplate restTemplate = RestTemplateUtil.registerTemplate();
        HttpEntity<String> request = new HttpEntity<>(payload, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        System.out.println(response.getBody());

        return response.getBody();
    }


}
